package com.ssmTest.controller;

import com.ssmTest.model.EmploymentAd;
import com.ssmTest.model.Interviews;
import com.ssmTest.model.JobAd;
import com.ssmTest.service.DateTimeService;
import com.ssmTest.service.IEmploymentAdService;
import com.ssmTest.service.IInterviewsService;
import com.ssmTest.service.IJobAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ADController {
    @Autowired
    private IJobAdService iJobAdService;
    @Autowired
    private IEmploymentAdService iEmploymentAdService;

    @Autowired
    private IInterviewsService iInterviewsService;

    @Autowired
    private DateTimeService dateTimeService;

    //返回用户发布信息列表
    @RequestMapping("/jobADList.do")
    public String jobADList(ModelMap modelMap) {
        List<JobAd> list = null;
        list = iJobAdService.JobAdList(0);
        modelMap.addAttribute("list", list);
        return "jobAdList";
    }

    //用户信息审核成功
    @RequestMapping("/jobADSuccess.do")
    public String jobADSuccess(int id) {
        System.out.println(id);
        JobAd jobAd = iJobAdService.selectJobAd(id);
        System.out.println("啦啦啦");
        jobAd.setToexamine(1);
        if (iJobAdService.updateJobAd(jobAd))
            return "success";
        else
            return "fail";
    }

    //用户信息审核失败弹出界面
    @RequestMapping("/jobADReasionPage.do")
    public String jobADReasionPage(HttpSession session, ModelMap modelMap, int id) {
        session.setAttribute("uuuid", id);
        modelMap.addAttribute("url", "jobadFail.do");
        return "writeReasionPage";
    }

    //用户审核失败理由提交
    @RequestMapping("/jobadFail.do")
    public String jobadFail(HttpSession session, String reasion) {
        iJobAdService.updateInformationReasion(Integer.parseInt(session.getAttribute("uuuid").toString()), reasion);
        return "success";
    }

    //用户招聘修改
    @RequestMapping("/jobADExl.do")
    public String jobADExl(int id, ModelMap modelMap) {
        JobAd jobAd = iJobAdService.selectJobAd(id);
        modelMap.addAttribute("jobAd", jobAd);
        return "jobADExl";
    }

    //用户招聘删除
    @RequestMapping("/jobADDel.do")
    public String jobADDel(int id) {
        JobAd jobAd = iJobAdService.selectJobAd(id);
        if (jobAd != null) {
            iJobAdService.delJobAD(id);
            return "success";
        }
        return "fail";
    }

    //用户信息更新
    @RequestMapping("/jobADUpdate.do")
    public String jobADUpdate(JobAd jobAd) {
        JobAd _jobAd = iJobAdService.selectJobAd(jobAd.getId());
        if (_jobAd != null) {
            iJobAdService.updateJobAd(jobAd);
            return "success";
        }
        return "fail";
    }
    //-----------------------企业用户----------------------------------------------------

    //返回企业发布信息列表
    @RequestMapping("/employmentList.do")
    public String employmentList(ModelMap modelMap) {
        List<EmploymentAd> list = null;
        list = iEmploymentAdService.EmploymentAdList(0);
        modelMap.addAttribute("list", list);
        return "employmentList";
    }

    //企业信息审核成功
    @RequestMapping("/employmentSuccess.do")
    public String employmentSuccess(int id) {
        EmploymentAd employmentAd = iEmploymentAdService.selectEmploymentAd(id);
        employmentAd.setToexamine(1);
        if (iEmploymentAdService.updateEmploymentAd(employmentAd))
            return "success";
        else
            return "fail";
    }

    //企业信息审核失败弹出界面
    @RequestMapping("/employmentReasionPage.do")
    public String employmentReasionPage(HttpSession session, ModelMap modelMap, int id) {
        session.setAttribute("uuuuid", id);
        modelMap.addAttribute("url", "employmentFail.do");
        return "writeReasionPage";
    }

    //企业审核失败理由提交
    @RequestMapping("/employmentFail.do")
    public String employmentFail(HttpSession session, String reasion) {
        iEmploymentAdService.updateInformationReasion(Integer.parseInt(session.getAttribute("uuuuid").toString()), reasion);
        return "success";
    }

    //企业招聘修改
    @RequestMapping("/employmentExl.do")
    public String employmentExl(int id, ModelMap modelMap) {
        EmploymentAd employmentAd = iEmploymentAdService.selectEmploymentAd(id);
        modelMap.addAttribute("employmentAD", employmentAd);
        return "employmentExl";
    }

    //企业招聘删除
    @RequestMapping("/employmentDel.do")
    public String employmentDel(int id) {
        EmploymentAd employmentAd = iEmploymentAdService.selectEmploymentAd(id);
        if (employmentAd != null) {
            iEmploymentAdService.delEmployment(id);
            return "success";
        }
        return "fail";
    }

    //企业信息更新
    @RequestMapping("/employmentUpdate.do")
    public String employmentUpdate(EmploymentAd employmentAd) {
        EmploymentAd _employmentAd = iEmploymentAdService.selectEmploymentAd(employmentAd.getId());
        if (_employmentAd != null) {
            iEmploymentAdService.updateEmploymentAd(employmentAd);
            return "success";
        }
        return "fail";
    }

    //打开招聘信息编辑界面
    @RequestMapping("/employmentPage.do")
    public String employmentPage() {
        return "outEmployment";
    }

    //后台为企业发布招聘信息
    @RequestMapping("/outEmployment.do")
    public String outEmployment(EmploymentAd employmentAd) {
        employmentAd.setTime(dateTimeService.getTime());
        employmentAd.setToexamine(1);
        employmentAd.setPersonal_id(1);
        iEmploymentAdService.insertEmploymentAd(employmentAd);
        return "success";
    }

    //----------------------------面试内容-------------------------------------

    //返回用户发布信息列表
    @RequestMapping("/interviewList.do")
    public String interviewList(ModelMap modelMap) {
        List<Interviews> list = iInterviewsService.interviewsList(0);
        modelMap.addAttribute("list", list);
        return "interviewList";
    }

    //报名信息确认
    @RequestMapping("/interviewSuccess.do")
    public String interviewSuccess(int id) {
        Interviews interviews = iInterviewsService.selectInterviewsByID(id);
        if (interviews != null)
            interviews.setToexamine(1);
        else
            return "fail";
        iInterviewsService.updateInterview(interviews);
        return "success";
    }

    //报名信息删除
    @RequestMapping("/interviewDel.do")
    public String interviewDel(int id) {
        Interviews interviews = iInterviewsService.selectInterviewsByID(id);
        if (interviews != null) {
            iInterviewsService.delInterview(id);
            return "success";
        }
        return "fail";
    }

    //添加备注弹出界面
    @RequestMapping("/interviewReasionPage.do")
    public String interviewReasionPage(HttpSession session, ModelMap modelMap, int id) {
        session.setAttribute("rid", id);
        modelMap.addAttribute("url", "interviewReamrk.do");
        return "writeReasionPage";
    }

    //用户审核失败理由提交
    @RequestMapping("/interviewReamrk.do")
    public String interviewReamrk(HttpSession session, String reasion) {
        iInterviewsService.updateInterviewReasion(Integer.parseInt(session.getAttribute("rid").toString()), reasion);
        return "success";
    }

}
